﻿using System;
using System.Collections.Generic;
using System.Text;
using HotelManagement.Common;
using HotelManagement.Common.SQL;
using System.Data.Common;
using HotelManagement.DL.Helper;
using System.Data;

namespace HotelManagement.DL.DAO
{
    public class UserDL
    {
        private readonly string SQL_PARA_NAME = "@name";
        private readonly string SQL_PARA_ID = "@id";

        public int Search(ref User user)
        {
            int nResult = (int)ReturnType.SUCCESS;
            try
            {
                DBHelper dbHelper = DBHelperFactory.createDBHelper();
                DbCommand command = null;
                if (string.IsNullOrEmpty(user.Name))
                {
                    command = dbHelper.CreateCommandBySql(SqlFile.SQL_SEARCH_USER_INFO);
                }
                else
                {
                    command = dbHelper.CreateCommandBySql(SqlFile.SQL_SEARCH_USER_INFO_BYNAME);
                    dbHelper.AddInParameter(command, SQL_PARA_NAME, DbType.String, user.Name);
                }

                DataSet ds = dbHelper.ExecuteDataSetQuery(command);
                if (0 == ds.Tables.Count)
                {
                    nResult = (int)ReturnType.FAILED;
                }
                user.UserCollection = ds.Tables[0];
            }
            catch (Exception)
            {
                throw;
            }

            return nResult;
        }

        public int UpdateUser(User user)
        {
            int nResult = (int)ReturnType.SUCCESS;
            try
            {
                DBHelper dbHelper = DBHelperFactory.createDBHelper();
                DbCommand command = dbHelper.CreateCommandBySql(SqlFile.SQL_UPDATE_USER);
                dbHelper.AddInParameter(command, "userName", DbType.String, user.Name);
                dbHelper.AddInParameter(command, "pwd", DbType.String, user.Password);

                dbHelper.AddInParameter(command, SQL_PARA_ID, DbType.String, user.ID);

                nResult = dbHelper.ExecuteNonQuery(command);
            }
            catch (Exception)
            {
                throw;
            }

            return nResult;
        }

        public int AddUser(User user)
        {
            int nResult = (int)ReturnType.SUCCESS;
            try
            {
                DBHelper dbHelper = DBHelperFactory.createDBHelper();
                DbCommand command = dbHelper.CreateCommandBySql(SqlFile.SQL_ADD_USER);
                dbHelper.AddInParameter(command, "UserName", DbType.String, user.Name);
                dbHelper.AddInParameter(command, "password", DbType.String, user.Password);

                nResult = dbHelper.ExecuteNonQuery(command);
            }
            catch (Exception)
            {
                throw;
            }

            return nResult;
        }

        public int DeletUser(User user)
        {
            int nResult = (int)ReturnType.SUCCESS;
            try
            {
                DBHelper dbHelper = DBHelperFactory.createDBHelper();
                DbCommand command = dbHelper.CreateCommandBySql(SqlFile.SQL_DELETE_USER);
                dbHelper.AddInParameter(command, SQL_PARA_NAME, DbType.String, user.ID);

                nResult = dbHelper.ExecuteNonQuery(command);
            }
            catch (Exception)
            {
                throw;
            }

            return nResult;
        }
    }
}
